/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.loggerWS;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import java.io.*;
import java.util.*;
import java.text.*;

/**
 *
 * @author Diego
 */
@WebService()
public class LoggerWS {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "logOrder")
    public void logOrder(@WebParam(name = "itemList")ItemShippingStatusList itemList)
    {  
        //Get Current Data.
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        //Log customer information
         try {

             PrintWriter out = new PrintWriter(new FileWriter("log.txt", true));//open-write to file
             out.println("Date of Order:"+dateFormat.format(date)+"To Cusomer:"+itemList.getCust().getName()+" With Customer Number:"+itemList.getCust().getCustomerReferenceNumber()+" From:"+itemList.getCust().getCountry()+"-"+itemList.getCust().getCity()+"-"+itemList.getCust().getZip()+" With Address:"+itemList.getCust().getStreet1()+"-"+itemList.getCust().getStreet2());
                
                //ItemsList
            if(itemList.getItemList().size()>0)
            {                          
                out.println("Items Ordered:");
                //Loop through itemList
                for (int i=0; i < itemList.getItemList().size(); i++)
                {
                    //Write to log file
                    out.println("Item "+i+":");
                     out.print(itemList.getItemList().get(i).getManufacturerName()+" :");
                     out.print(itemList.getItemList().get(i).getProductType()+" :");
                     out.print(itemList.getItemList().get(i).getQuantity()+" :");
                     out.print(itemList.getItemList().get(i).getUnitPrice()+" :");
                     out.println();//Go to next Line.
                 }                           
            }
            //NotShippedItemsList
            if(itemList.getNotShippedItems().size()>0)
            {
                out.println("Not shipped Items:");
                //Loop through itemList
                for (int i=0; i < itemList.getNotShippedItems().size(); i++)
                {
                    //Write to log file
                     out.println("Not shipped Item "+i+":");
                     out.print(itemList.getNotShippedItems().get(i).getManufacturerName()+" :");
                     out.print(itemList.getNotShippedItems().get(i).getProductType()+" :");
                     out.print(itemList.getNotShippedItems().get(i).getQuantity()+" :");
                     out.print(itemList.getNotShippedItems().get(i).getUnitPrice()+" :");
                     out.println();//Go to next Line.
                 }
              
            }
            //ShippedItemsList
            if(itemList.getShippedItems().size()>0)
            {
                 out.println("Shipped Items:");
                    //Loop through itemList
                    for (int i=0; i < itemList.getShippedItems().size(); i++)
                    {
                        //Write to log file
                         out.println("Shipped Item "+i+":");
                         out.print(itemList.getShippedItems().get(i).getManufacturerName()+" :");
                         out.print(itemList.getShippedItems().get(i).getProductType()+" :");
                        out.print(itemList.getShippedItems().get(i).getQuantity()+" :");
                         out.print(itemList.getShippedItems().get(i).getUnitPrice()+" :");
                         out.println();//Go to next Line.
                         //Leave two spaces between orders.
                      out.println();out.println();
                     }
            }
        out.close();//close file
        }
        catch (IOException e)  {System.out.println("An error has occured!!!!");}//close file



    }

}
